package com.ming.transaction.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ming.transaction.common.Result;
import com.ming.transaction.entity.Complain;
import com.ming.transaction.mapper.ComplainMapper;
import com.ming.transaction.service.IComplainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.concurrent.TimeUnit;

import static com.ming.transaction.common.Constant.COMPLAIN_TTL;
import static com.ming.transaction.common.Constant.COMPLAIN_USER;

/**
 * <p>
 * 信息反馈表 服务实现类
 * </p>
 *
 * @author 陈仕健
 * @since 2023-05-12
 */
@Service
public class ComplainServiceImpl extends ServiceImpl<ComplainMapper, Complain> implements IComplainService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 保存投诉信息
     * @param complain
     * @return
     */
    @Override
    public Result saveComplain(Complain complain) {
        String unumber = complain.getUnumber();
        String s = stringRedisTemplate.opsForValue().get(COMPLAIN_USER + unumber);
        if (StrUtil.isNotBlank(s)){
            return Result.fail("意见提交失败,每日只能提交一次!");
        }
        stringRedisTemplate.opsForValue().set(COMPLAIN_USER+unumber,"1",COMPLAIN_TTL, TimeUnit.DAYS);
        if (save(complain)) {
            return Result.success("意见提交成功!");
        }
        return Result.fail("未知服务错误!");
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Result pageSeach(Integer pageNum, Integer pageSize) {
        IPage<Complain> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Complain> queryWrapper = new QueryWrapper<>();
        return Result.success("意见信息查询成功!",page(page,queryWrapper));
    }
}
